import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

/// * 作者: Tao
/// * 日期: 2024/3/20
/// * 时间: 10:05
/// * 描述: 基类，用于绑定Controller
abstract class GetBindingView<T extends GetxController> extends StatefulWidget{
  const GetBindingView({super.key});

  final String? tag = null;

  /// 根据tag绑定Controller
  T get controller => GetInstance().find<T>(tag: tag);

  @protected
  Widget build(BuildContext context);

  @protected
  Bindings? binding();

  @override
  AutoDisposeState createState() => AutoDisposeState<T>();
}

/// * 作者: Tao
/// * 日期: 2024/3/20
/// * 时间: 10:05
/// * 描述: 基类，可自动装载的状态管理
class AutoDisposeState<S extends GetxController> extends State<GetBindingView>{
  AutoDisposeState();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<S>(
      builder: (controller) {
        return widget.build(context);
      },
    );
  }

  @override
  void initState() {
    super.initState();
    widget.binding()?.dependencies();
  }

  @override
  void dispose() {
    Get.delete<S>();
    super.dispose();
  }
}